<script>
	$(document).ready(function(){
		if(<?php echo $node ?> != "0")
			window.setInterval(saveContent, 60000);

		$('.levelFaces').change(function() {		
			var url = '<?php echo WWW_APP."units/edit/unit/".$id."/node/".$node."/level/"; ?>';

			window.location.href= url + $(this).val();			 
		});		

			

		CKEDITOR.replace( 'oaeditor',
		{
			toolbar :
			[
				['Source','-','DocProps','Preview','Print','-','Templates'],
				['Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo'],
				['Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt'],
				['Maximize', 'ShowBlocks'],
				['Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat'],
				['NumberedList','BulletedList','-','Outdent','Indent','Blockquote','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl'],
				['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
				['Styles','Format','Font','FontSize'],
				['TextColor','BGColor'],
				['Link', 'Unlink']
			]
		});

		$("#bt_vision").click(function() {

			$.blockUI({ message: $('<p style="font-size:15px;">Aguarde Carregando...</p>') });
			var data = CKEDITOR.instances['oaeditor'].getData();
			
			if(data) {
				$.ajax({
				   type: "POST",
				   url: "<?php echo WWW_APP.'units/saveContent/';?>",
				   data: {unit : <?php echo $id?>, node : <?php echo $node?>, level : $('.levelFaces').val(), content : data},
				   success: function($msg){
					 
				   }
				});
			}

			var vision = '<?php echo WWW_APP.'units/vision/unit/'.$id.'/node/'.$node?>';
			window.location.href = vision;											
		});
			
	});
	
	function novoConceito() {
		var oaeditor = CKEDITOR.instances['oaeditor'];
		var textSelected = oaeditor.document.getSelection().getSelectedText();
		
		if(textSelected) {
			$.blockUI({ message: $('<p style="font-size:15px;">Aguarde Carregando...</p>') });
			$.ajax({
			   type: "POST",
			   url: "<?php echo WWW_APP.'units/newConcept/';?>",
			   data: {id_unit : <?php echo $id?>, id_node : <?php echo $node?>, name_node : textSelected},
			   success: function(data){
				  var data_decode = data.split('&&');				  
				  var text = oaeditor.getData();
				  var url = "<?php echo WWW_APP.'units/vision/unit/'.$id.'/node/';?>";				  
				  var link = "<a class='concept' href='"+url+data_decode[0]+"'>" + textSelected + "</a>";
				  text = text.replace(new RegExp(data_decode[2], 'g'), link); 		
				  oaeditor.setData(text);
				  
				  $('#sumary').html(data_decode[1]);
				  $("#browser").treeview();		
				  		  
				  $.unblockUI();
			   }
			});			
		}else {
			var id = "<?php echo $id?>";
			var node = "<?php echo $node?>";
			
			var conteudoHTML = '<form id="form" method="">' +
				'<label class="label-nome">Nome do Item: *</label>' + 
				'<input type="text" id="name_node" class="input-name"/>' +
				'</form>';
			
			$.confirm({
				'title' : 'Adicionar Conceito',
				'message' : conteudoHTML,
				'buttons' : {
					'ok' : {
						'class' : 'blue',
						'action' : function() {
							$.blockUI({ message: $('<p style="font-size:15px;">Aguarde Carregando...</p>') });
							$.ajax({
							   type: "POST",
							   url: "<?php echo WWW_APP.'units/newConcept/';?>",
							   data: {id_unit : id, id_node : node, name_node : $('#name_node').val()},
							   success: function(data){
								  var data_decode = data.split('&&');
								  $('#sumary').html(data_decode[1]);
								  $("#browser").treeview();
								  $.unblockUI();
								  document.location.reload();
							   }
							});	
						}
					},
					'cancelar' : {
						'class' : 'gray',
						'action' : function() {
							return false;
						}
					}
				}
			});
		}

	}

	function saveContent(){
		var data = CKEDITOR.instances['oaeditor'].getData();
		
		if(data) {
			$.ajax({
			   type: "POST",
			   url: "<?php echo WWW_APP.'units/saveContent/';?>",
			   data: {unit : <?php echo $id?>, node : <?php echo $node?>, level : <?php echo $level?>, content : data},
			   success: function($msg){
				 $('#saveMsg').html($msg);
				 $("#saveMsg").fadeIn();
	    		 window.setTimeout(escondeMsg, 15000);
			   }
			});
		}
		else {
			$('#saveMsg').html('Adicione conteúdo ao ítem.');
			$("#saveMsg").fadeIn();
   		 	window.setTimeout(escondeMsg, 6000);
		}
	}

	function escondeMsg() {   		
		$("#saveMsg").fadeOut();
	}
</script>
<form id="faces" action="javascript:void(0);">
	<select onchange="javascript:saveContent();" class="levelFaces" <?php if($node == 0){echo 'disabled="disabled"';}?> >
		<option value="1" <?php if($level == 1){echo 'selected="selected"';}?> >Sintético</option>
		<option value="2" <?php if($level == 2){echo 'selected="selected"';}?> >Detalhado</option>
		<option value="3" <?php if($level == 3){echo 'selected="selected"';}?> >Saiba Mais</option>
	</select>
</form>

<div id="editor">
	<div class="links">
		<input type="button" onclick="javascript:novoConceito();saveContent();" value="Novo Conceito" />
		<a onclick="javascript:saveContent();" href="<?php echo WWW_APP.'questions/index/unit/'.$id?>">Questionário</a>
	</div>
				
	<form action="javascript:void(0);">
		<textarea id="oaeditor" name="oaeditor" rows="16" cols="91"	 >
			<?php if(isset($content)){ echo $content; }?>
		</textarea>
	</form>	
	<div class="links">		
		<input type="button" onclick="javascript:saveContent();" value="Salvar"  />		
		<a class="delete" href="<?php echo WWW_APP.'units/delete/unit/'.$id.'/node/'.$node?>">Excluir</a>
		<a onclick="javascript:saveContent();" id="bt_vision" href="#">Visualizar</a>
	</div>
	
	<div id="saveMsg"></div>
</div>

<div id="sumary">
	<?php echo $treeview;?>
</div>

<br class="clear" />